package campus.aiit.ssl.controller;

import campus.aiit.ssl.service.StatisticsService;
import campus.aiit.ssl.util.DateFormatUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.*;

@Api(description = "统计相关api")
@RestController
public class StatisticsController {
    @Resource
    private StatisticsService statisticsService;

    @ApiOperation("获取不同类型设备的数量")
    @GetMapping({"getDeviceNum"})
    public Map<String,Object> getDeviceNum(Long blockId,Long deviceTypeId){
        Map<String , Object> map = new HashMap<>();
        map.put("totalDeviceNum",statisticsService.getDeviceNum(blockId,deviceTypeId));
        map.put("deviceNumOfBlock",statisticsService.DeviceNumOfBlock(blockId,deviceTypeId));
        return map;
    }

    @ApiOperation("获取所有区域块统计用电量")
    @GetMapping({"getECOfAllBlock"})
    public Map<String,Object> getECOfAllBlock(){
        Map<String , Object> map = new HashMap<>();
        map.put("total",statisticsService.getTotalECOfAllBlock());
        map.put("ECOfBlock",statisticsService.getECOfBlock(null,null,null,null));
        return map;
    }

    @ApiOperation("统计设备用电量")
    @GetMapping({"getECOfDevice"})
    public Map<String,Object> getECOfDevice(Long blockId,Long deviceTypeId,String startTime,String endTime){
        DateFormatUtil time = new DateFormatUtil();
        startTime = time.format(startTime);
        endTime = time.format(endTime);

        Map<String , Object> map = statisticsService.getECOfDevice(blockId,deviceTypeId,startTime,endTime);
        if(blockId==null){
            map.put("totalECOfBlock",statisticsService.getTotalECOfBlock(blockId,deviceTypeId,startTime,endTime));
            map.put("DayECOfBlock",statisticsService.getDayECOfBlock(blockId,deviceTypeId,startTime,endTime));
        }else {
            map.put("DayECOfDeviceType",statisticsService.getDayECOfDeviceType(blockId,deviceTypeId,startTime,endTime));
        }

        return map;
    }

    @ApiOperation("按设备类型统计用电量,获取所有设备类型当(周/月/季/年)用电量与总用电量")
    @GetMapping({"getECOfTime"})
    public Map<String,Object> getECOfTime() {

        Map<String , Object> map = new HashMap<>();
        String[] weeks = {"周一","周二","周三","周四","周五","周六","周日"};
        String nowTime = new SimpleDateFormat("yyyy-MM-dd-EE").format(new Date());
        String year = nowTime.substring(0,4);
        String month = nowTime.substring(5,7);
        String week = nowTime.substring(11,13);
        String firstDayOfYear = year+"-01-01";
        String firstDayOfMonth = year+"-"+month+"-01";
        String firstDayOfSeason,firstDayOfWeek = null;
        if (Integer.parseInt(month) <4){
            firstDayOfSeason = year+"-01-01";
        }else if(Integer.parseInt(month) <7){
            firstDayOfSeason = year+"-04-01";
        }else if(Integer.parseInt(month) <10){
            firstDayOfSeason = year+"-07-01";
        }else {
            firstDayOfSeason = year+"-10-01";
        }
        for(int i=0;i<weeks.length;i++){
            if(weeks[i].equals(week)){
                firstDayOfWeek = new SimpleDateFormat("yyyy-MM-dd").format(new Date().getTime()-i*24*60*60*1000);
            }
        }

        map.put("total",statisticsService.getTotalECOfType(null,null,null,null));
        map.put("currentWeek",statisticsService.getECOfDeviceType(null,null,firstDayOfWeek,null));
        map.put("currentMonth",statisticsService.getECOfDeviceType(null,null,firstDayOfMonth,null));
        map.put("currentSeason",statisticsService.getECOfDeviceType(null,null,firstDayOfSeason,null));
        map.put("currentYear",statisticsService.getECOfDeviceType(null,null,firstDayOfYear,null));

        return map;
    }

    @ApiOperation("通过设备ID统计每月用电量")
    @GetMapping({"getMonthECByDeviceId"})
    public Map<String , Object> getMonthECByDeviceId(Long deviceId,String startTime,String endTime) {
        DateFormatUtil time0 = new DateFormatUtil();
        startTime = time0.format(startTime);
        endTime = time0.format(endTime);

        Map<String , Object> map = new HashMap<>();
        Map<String , Object> total = new HashMap<>();
        String nowTime = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
        String year = nowTime.substring(0,4);
        String month = nowTime.substring(5,7);
        String startMonth = startTime.substring(5,7);
        List<Map<String , Object>> list = new ArrayList<Map<String, Object>>();
        int maxEC = -1;
        int minEC = Integer.MAX_VALUE;
        String maxECMonth = "",minECMonth = "";
        int totalEC = 0;
        for(int i=Integer.parseInt(startMonth);i<=Integer.parseInt(month);i++){
            String time = year+"-"+String.format("%02d",i)+"-01";
            String nextMonth = null;
            if(i!=Integer.parseInt(month)){
                nextMonth = year+"-"+String.format("%02d",i+1)+"-01";
            }
            Map<String , Object> map1 = new HashMap<>();
            map1.put("month",year+"-"+i);
            int EC = statisticsService.getMonthECByDeviceId(deviceId,time,nextMonth);
            map1.put("EC",EC);
            if(EC > maxEC){
                maxEC = EC;
                maxECMonth = year+"-"+i;
            }
            if(EC < minEC){
                minEC = EC;
                minECMonth = year+"-"+i;
            }
            totalEC += EC;
            list.add(map1);
        }
        total.put("maxEC",maxEC);
        total.put("maxECMonth",maxECMonth);
        total.put("minEC",minEC);
        total.put("minECMonth",minECMonth);
        total.put("totalEC",totalEC);
        total.put("avgEC",(float)totalEC/(Integer.parseInt(month)-Integer.parseInt(startMonth)+1));
        map.put("total",total);
        map.put("ECList",list);
        return map;
    }

    @ApiOperation("统计本年每月用电量")
    @GetMapping({"getMonthEC"})
    public Map<String , Object> getMonthEC() {

        String nowTime = new SimpleDateFormat("yyyy-MM").format(new Date());
        String year = nowTime.substring(0,4);
        String month = nowTime.substring(5,7);
        List<Map<String , Object>> list = new ArrayList<Map<String, Object>>();

        for(int i=1;i<=Integer.parseInt(month);i++){
            String time = year+"-"+String.format("%02d",i)+"-01";
            String nextMonth = null;
            if(i!=Integer.parseInt(month)){
                nextMonth = year+"-"+String.format("%02d",i+1)+"-01";
            }
            Map<String , Object> map = new HashMap<>();
            map.put("month",year+"-"+i);
            map.put("ECOfType",statisticsService.getECOfDeviceType(null,null,time,nextMonth));
            list.add(map);
        }
        Map<String , Object> map = new HashMap<>();
        List<Map<String , Object>> tempList = new ArrayList<Map<String, Object>>();
        map.put("total",statisticsService.totalEC(list));
        map.put("ECList",list);
        return map;
    }

    @ApiOperation("统计本年每季用电量")
    @GetMapping({"getSeasonEC"})
    public Map<String , Object> getSeasonEC() {

        String nowTime = new SimpleDateFormat("yyyy-MM").format(new Date());
        String year = nowTime.substring(0,4);
        String month = nowTime.substring(5,7);
        List<Map<String , Object>> list = new ArrayList<Map<String, Object>>();
        String[] season = {"2020-01-01","2020-04-01","2020-07-01","2020-10-01"};
        int len = Integer.parseInt(month);
        if(len%3>0){
            len = len/3 +1;
        }
        for(int i=0;i<len;i++){
            String startTime = season[i];
            String endTime = null;
            if(i+1!=len){
                endTime = season[i+1];
            }
            Map<String , Object> map = new HashMap<>();
            map.put("season",i+1);
            List<Map<String,Object>> ECOfType = statisticsService.getECOfDeviceType(null,null,startTime,endTime);
            map.put("ECOfType",ECOfType);
            list.add(map);
        }
        Map<String , Object> map = new HashMap<>();
        map.put("total",statisticsService.totalEC(list));
        map.put("ECList",list);
        return map;
    }

    @ApiOperation("统计近几年用电量")
    @GetMapping({"getYearEC"})
    public Map<String , Object> getYearEC(@RequestParam(defaultValue = "5") int year) {

        String nowYear = new SimpleDateFormat("yyyy").format(new Date());
        String temp = "-01-01";
        List<Map<String , Object>> list = new ArrayList<Map<String, Object>>();
        int startYear = Integer.parseInt(nowYear)-year+1;

        for(int i=startYear;i<=Integer.parseInt(nowYear);i++){
            String startTime = i + "-01-01";;
            String endTime = null;
            if(i!=Integer.parseInt(nowYear)){
                endTime = (i+1) + "-01-01";
            }
            Map<String , Object> map = new HashMap<>();
            map.put("year",i);
            map.put("ECOfType",statisticsService.getECOfDeviceType(null,null,startTime,endTime));
            list.add(map);
        }

        Map<String , Object> map1 = new HashMap<>();
        map1.put("total",statisticsService.totalEC(list));
        map1.put("ECList",list);
        return map1;
    }

}
